package com.ys.controller.web.account;

import com.ys.config.MenuId;
import com.ys.controller.api.sys.ApiInterYsGridButtonController;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.account.TcwAdvanceAccountBack;
import com.ys.entity.sys.YsGridButton;
import com.ys.service.account.TcwAdvanceAccountBackService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/web/account/tcwAdvanceAccountBack")
public class WebTcwAdvanceAccountBackController {


	@Autowired
	private CommonApiService<TcwAdvanceAccountBack> commonApiService;
	@Autowired
	private TcwAdvanceAccountBackService tcwAdvanceAccountBackService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_AdvanceAccountBack);
			TcwAdvanceAccountBack tcwAdvanceAccountBack = new TcwAdvanceAccountBack()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());


			return commonApiService.toAdd(model, tcwAdvanceAccountBack, MenuId.TCW_AdvanceAccountBack, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		if (params.get("bid") == null){
			return W.error(model, "单号不能为空");
		}

		String bid = (params.get("bid").toString());
		try {
			Map<String,Object> entity = tcwAdvanceAccountBackService.getExtraParam(bid);
			if (entity == null){
				return W.error(model, "预支账户归还单不存在!");
			}

			return commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TcwAdvanceAccountBack(), MenuId.TCW_AdvanceAccountBack, entity, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
}
